var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect, useRef, forwardRef, useImperativeHandle, useState } from 'react';
import Mescroll from 'mescroll.js';
import 'mescroll.js/mescroll.min.css';
import { debounce } from 'yjr-utils';
function MescrollReact(_a, ref) {
    var _b = _a.dropDownCb, dropDownCb = _b === void 0 ? function () { } : _b, _c = _a.upGlideCb, upGlideCb = _c === void 0 ? function () { } : _c, className = _a.className, style = _a.style, children = _a.children, _d = _a.upGlideOffset, upGlideOffset = _d === void 0 ? 100 : _d;
    var dom = useRef(null);
    var instance = useRef(null);
    var _e = useState(false), tog = _e[0], setTog = _e[1];
    useEffect(function () {
        instance.current = new Mescroll(dom.current, {
            down: {
                // 必须在顶部才可以下拉刷新
                mustToTop: false,
                // 关闭上来就自动执行一次
                auto: false,
                // 不允许ios的bounce回弹
                isBounce: false,
                callback: debounce(dropDownCb, 500)
            },
            up: {
                auto: false,
                isBounce: false,
                offset: upGlideOffset,
                callback: debounce(upGlideCb, 500),
                htmlNodata: "<p class=\"upwarp-nodata\">\u5230\u5E95\u4E86\uFF5E\uFF5E </p>"
            }
        });
        setTog(true);
        return function () { var _a; return (_a = instance.current) === null || _a === void 0 ? void 0 : _a.destroy(); };
    }, []);
    useImperativeHandle(ref, function () {
        var tg = instance.current;
        return {
            endDropDown: function () { return tg === null || tg === void 0 ? void 0 : tg.endSuccess(); },
            endUpGlide: function (hasMore) {
                if (hasMore === void 0) { hasMore = true; }
                return tg === null || tg === void 0 ? void 0 : tg.endSuccess(10, hasMore);
            },
            scrollTo: function (yAxis, time) { return tg === null || tg === void 0 ? void 0 : tg.scrollTo(yAxis, time); }
        };
    }, [tog]);
    return (_jsx("div", __assign({ ref: dom, className: className === null || className === void 0 ? void 0 : className.concat(' mescroll'), style: style }, { children: _jsx("div", { children: children }) })));
}
export default forwardRef(MescrollReact);
